#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int Position;
typedef struct SNode* PtrToSNode;
struct SNode {
    int* Data;
    Position Top;
    int Maxsize;
};
typedef PtrToSNode Stack;

Stack CreateStack ( int MaxSize );

bool IsFull ( Stack S );

bool Push ( Stack S, int X );

bool IsEmpty ( Stack S );

int Pop ( Stack S );

int main(void) {
    Stack S = CreateStack(10);
    int choice, X, result, i;
    while(1) {
        printf("Stack\r\nPlease input\r\n1 Push\t2 Pop\n3 Print\t4 Exit\n");
        scanf("%d",&choice);
        switch (choice) {
        case 1:
            if ( IsFull(S) ) {
                printf("Full\n");
                exit(-9);
            }
            printf("Please input\n");
            scanf("%d",&X);
            if ( Push(S,X) ) printf("Success Push\n");
            break;
        case 2:
            if ( IsEmpty(S) ) {
                printf("Empty\n");
                exit(-9);
            }
            result = Pop(S);
            printf("Pop\t%d\n",result);
            break;
        case 3:
            printf("data\n");
            for ( i = 0; i < 10; i++ ) printf("%d\t|\t%d\n",i+1,S->Data[i]);
            break;
        case 4:
            free(S->Data);
            free(S);
            printf("Free success!\n");
            exit(0);
            break;
        }
    }
    return 0;
}

Stack CreateStack ( int MaxSize ) {
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (int*)malloc(MaxSize*sizeof(int));
    S->Top = -1;
    S->Maxsize = MaxSize;

    return S;
}

bool IsFull ( Stack S ) {
    return ( S->Top == S->Maxsize - 1 );
}

bool Push ( Stack S, int X ) {
    if (IsFull(S)) {
        printf("Is Full\n");
        return false;
    }
    S->Data[++(S->Top)] = X;
    return true;
}

bool IsEmpty ( Stack S ) {
    return ( S->Top == -1 );
}

int Pop ( Stack S ) {
    if ( IsEmpty(S) ) {
        printf("Empty\n");
        return -1;
    }
    return ( S->Data[(S->Top)--] );
}